import 'package:flutter/services.dart';
typedef CertifyCallBack = void Function(String action,String reason);
class CertifyPlugin {
  static const String _CHANNEL_GCYW_CERTIFY = "gcyw/certify";
  static const MethodChannel _methodChannel = const MethodChannel(_CHANNEL_GCYW_CERTIFY);
  CertifyCallBack callBack;
  factory CertifyPlugin() => _getInstance();
  static CertifyPlugin _instance;

  CertifyPlugin._internal();

  static CertifyPlugin _getInstance() {
    if (_instance == null) {
      _instance = CertifyPlugin._internal();
    }
    return _instance;
  }

  void init() {
    _methodChannel.setMethodCallHandler(_onMethodCall);
  }

  ///获取认证环境参数
  Future<String> getMetaInfo() async {
    try {
      return await _methodChannel.invokeMethod('getMetaInfo');
    } on PlatformException {
      return '';
    }
  }

  ///开始认证
  Future<dynamic> startCertify(String certifyId,CertifyCallBack func) async {
    this.callBack = func;
    try {
      await _methodChannel.invokeMethod('startCertify', {"certifyId": certifyId});
    } on PlatformException {
      return false;
    }
  }

  Future<dynamic> _onMethodCall(MethodCall call) async{
    switch(call.method){
      case "onError":
        return _onError(call.arguments);
        break;
      case "onSuccess":
        return _onSuccess(call.arguments);
    }
  }
  _onError(dynamic tip){
    this.callBack("onError",tip);
  }
  _onSuccess(dynamic tip){
    this.callBack("onSuccess",tip);
  }
}
